<?php
namespace app\yunda\home;

use app\yunda\model\Qiandao as QiandaoModel;
use app\yunda\model\User;
use app\yunda\model\ScoreRecord;
use think\Db;

/**
 * 用户签到
 * @package app\yunda\home
 */
class Qiandao1 extends Base {
    // 获取列表
    public function index($openid=''){
        if(empty($openid))return mogo_error('参数错误',404);
        // 查询字段
        $map = $this->getMap();
        $map['get_score']=config('qian_dao_score')['m'];
        $map['openid']=$openid;
        $Address = new QiandaoModel();
        // 自定义where查询
        $data =  $Address
            ->order('id desc')
            ->where($map)
            ->paginate()
            ->each(function($item){
                $item['create_time']=format_time($item['create_time'],'Y.m.d');
                $item['day_num']='连续签到第'.$item['day_num'].'天';
                return $item;
            });
        $data = $data->toArray();

        $row=User::get(['openid'=>$openid]);
        $row_1=QiandaoModel::where('openid',$openid)->order('create_time')->find();
        $row_2=QiandaoModel::where('openid',$openid)->whereTime('create_time','d')->find();//今日是否签到
        if(empty($row_1)) $row_1['day_num']=0;

        $extra=[
            'score'=>$row['score'],
            'day'=>$row_1['day_num'],
            'is_sign'=>count($row_2)? 1 : 0,
        ];
        return mogo_json($data,'',$extra);
    }
    // 签到
    public function save(){
        // 第一步 过滤字段
        $data = request()->only(['openid']);
        // 第二部 验证字段
        $result = $this->validate($data,'Qiandao');
        if(true!==$result){
            return mogo_error($result);
        }
        $row=Db::table('dp_yd_qiandao')
            ->whereTime('create_time','d')
            ->where(['openid'=>$data['openid']])
            ->find();
        if(!empty($row)){
            return mogo_error('您今天已经签到',404);
        }
        $user=User::get(['openid'=>$data['openid']]);
        //获取用户是否初次签到
        $list=QiandaoModel::where(['openid'=>$data['openid']])->order('create_time desc')->select();

        //上次签到时间
        if(empty($list)) $data['last_qd_time']=time();
        $data['last_qd_time']=$list[0]['create_time'];

        //获取设定连续签到n天获得m积分
        $config=config('qian_dao_score');
        //获取用户到今天的连续签到天数
        $num=1;
        foreach ($list as $k=>$v){
            $c_day=$v['create_time']-$v['last_qd_time'];
            $day=86400;//一天的秒数
            $s=$c_day/86400;
            if($s<=1){
                $num+=1;
            } else{
                break;
            }
        }
        $yushu=$num%$config['n'];
        $data['day_num']=$yushu;
        if($yushu==0){
            $data['get_score']=$config['m'];
        }else{
            $data['get_score']=0;
        }
       // halt($data);
        $res = QiandaoModel::create($data);
        //更新用户积分
        $user['score']+=$res['get_score'];
        $user->update(['score'=>$user['score'],'id'=>$user['id']]);
        $recharge='签到获得积分'.$res['get_score'];
        ScoreRecord::createScoreRecord($recharge,$res['get_score'],$user['id']);
        if($res['get_score']==0)return mogo_json($res['day_num'],'',1);
        return mogo_json($res,'',1);
    }










}